package cbbx_sm.evaluation;

import java.io.IOException;
import java.util.List;
import java.util.Vector;

import cbbx_sm.decision_maker.DecisionMakerFactory;
import cbbx_sm.decision_maker.IDecisionMaker;
import cbbx_sm.parser.CameraData;
import cbbx_sm.parser.Parser;
import cbbx_sm.probabilistic_model.Cluster;
import cbbx_sm.probabilistic_model.IPredictor;
import cbbx_sm.probabilistic_model.NoisyOrPredictor;
import cbbx_sm.probabilistic_model.ProbabilisticModel;
import cbbx_sm.probabilistic_model.SimpleHeuristicPredictor;
import cbbx_sm.probabilistic_model.SystemShortTermMemory;
import cbbx_sm.simulation.Scheduler;
import cbbx_sm.utils.Utility;

public class HeuristicVSNoisyOr {
	public static void main(String[] args) throws IOException, InterruptedException{
		CameraData data16 = Parser.parseFile("data/cameraData/2009052816.txt", "cam16");
		CameraData data17 = Parser.parseFile("data/cameraData/2009052817.txt", "cam17");
		CameraData data18 = Parser.parseFile("data/cameraData/2009052818.txt", "cam18");
		
		data16.setImageFile("data/images/16.jpg");
		data17.setImageFile("data/images/17.jpg");
		data18.setImageFile("data/images/18.jpg");
		
		List<Cluster> clusters16 = Utility.getClusters("data/clusterData/Kruskal_150.0_2009052816.txt", "cam16", data16);;
		List<Cluster> clusters17 = Utility.getClusters("data/clusterData/Kruskal_150.0_2009052817.txt", "cam17", data17);
		List<Cluster> clusters18 = Utility.getClusters("data/clusterData/Kruskal_150.0_2009052818.txt", "cam18", data18);;
		
		List<Cluster> fusedClusters = new Vector<Cluster>();
		
		fusedClusters.addAll(clusters16);
		fusedClusters.addAll(clusters17);
		fusedClusters.addAll(clusters18);
	
		List<CameraData> cameraData = new Vector<CameraData>();
		cameraData.add(data16);
		cameraData.add(data17);
		cameraData.add(data18);
		
		List<List<Cluster>> clusters = new Vector<List<Cluster>>();
		clusters.add(clusters16);
		clusters.add(clusters17);
		clusters.add(clusters18);
		
		System.out.println("NUMBER OF BBOX TO CATCH: " + (data16.getNumberOfFrameWithBoundingBoxes() +
							data17.getNumberOfFrameWithBoundingBoxes() + data18.getNumberOfFrameWithBoundingBoxes()));
		System.out.println("Starting calculating probs...");
//		ProbabilisticModel probModel = new ProbabilisticModel();
//		probModel.computeProbabilities(cameraData, fusedClusters, (18 - 13) * 60 * 60);
		ProbabilisticModel probModel = ProbabilisticModel.readUp("old_13-14-15-16-17-18_150Clust.ser");
//		probModel.setThreshold(0.06);
		System.out.println("Probability computed:");
		System.out.println();
		
		SystemShortTermMemory memory;
		IPredictor predictor;
		IDecisionMaker decisionMaker;
		Scheduler scheduler;
				
		scheduler = new Scheduler(cameraData, fusedClusters, clusters, probModel, "output/HeuristicVSNoisyOR_Heuristic.txt");
		
		for (double dec_threshold = 0.0; dec_threshold < 0.2; dec_threshold += 0.02){
			memory = new SystemShortTermMemory(15);
			predictor = new SimpleHeuristicPredictor(probModel, memory, fusedClusters);
			decisionMaker = DecisionMakerFactory.getMostLikelyClusterDecisionMaker(dec_threshold);
			
			scheduler.setDecisionMaker(decisionMaker);
			scheduler.setMemory(memory);
			scheduler.setPredictor(predictor);
			scheduler.sequentialRun();
			System.gc();
		}
		
		scheduler = new Scheduler(cameraData, fusedClusters, clusters, probModel, "output/HeuristicVSNoisyOR_Noisy_NOApriori.txt");
		
		for (double dec_threshold = 0.0; dec_threshold < 0.2; dec_threshold += 0.02){
			memory = new SystemShortTermMemory(5);
			predictor = new NoisyOrPredictor(memory, probModel, 5, fusedClusters, false);
			decisionMaker = DecisionMakerFactory.getMostLikelyClusterDecisionMaker(dec_threshold);
			
			scheduler.setDecisionMaker(decisionMaker);
			scheduler.setMemory(memory);
			scheduler.setPredictor(predictor);
			scheduler.sequentialRun();
			System.gc();
		}
		
		scheduler = new Scheduler(cameraData, fusedClusters, clusters, probModel, "output/HeuristicVSNoisyOR_Noisy.txt");

		for (double dec_threshold = 0.0; dec_threshold < 0.2; dec_threshold += 0.02){
			memory = new SystemShortTermMemory(5);
			predictor = new NoisyOrPredictor(memory, probModel, 5, fusedClusters, true);
			decisionMaker = DecisionMakerFactory.getMostLikelyClusterDecisionMaker(dec_threshold);
			
			scheduler.setDecisionMaker(decisionMaker);
			scheduler.setMemory(memory);
			scheduler.setPredictor(predictor);
			scheduler.sequentialRun();
			System.gc();
		}
		
	}
}
